package com.flashcards;

import android.util.Log;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import weibo4andriod.org.json.JSONObject;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Word {
    private String eng;
    private String translation;

    public static Word NULL = new Word("", "");
    public static Word NO_MORE = new Word("", "");

    public Word(String eng, String translation) {
        this.eng = eng;
        this.translation = translation;
    }

    public String getEng() {
        return eng;
    }

    public String getTranslation() {
        return this.translation;
    }

    public void addTo(List<Word> words) {
        if (this.equals(Word.NULL)) {
            return;
        }
        words.add(this);
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Word word = (Word) o;

        if (eng != null ? !eng.equals(word.eng) : word.eng != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return eng != null ? eng.hashCode() : 0;
    }


    public Word translate() {
        String url = "http://ajax.googleapis.com/ajax/services/language/translate";
        HttpClient httpClient = new HttpClient();
        try {
            GetMethod getMethod = new GetMethod(url);
            NameValuePair[] nameValuePairs = {
                    new NameValuePair("v", "1.0"),
                    new NameValuePair("q", this.getEng()),
                    new NameValuePair("langpair", "en|zh-CN")
            };
            getMethod.setQueryString(nameValuePairs);
            int i = httpClient.executeMethod(getMethod);
            return Word.parseFromGoolgeTranslation(this.getEng(), getMethod.getResponseBodyAsString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Word.NULL;
    }

    private static Word parseFromGoolgeTranslation(String english, String responseBodyAsString) {
        try {
            JSONObject jsonObject = new JSONObject(responseBodyAsString);
            JSONObject responseData = jsonObject.getJSONObject("responseData");
            return new Word(english, responseData.getString("translatedText"));
        } catch (Exception e) {
            return Word.NULL;
        }
    }

    public static Word parseFromTwitteSyntax(String text) {

        Pattern reg = Pattern.compile("([a-zA-Z]+)\\s*@单词本");
        Matcher matcher = reg.matcher(text.trim());
        if (matcher.matches()) {
            Log.i(">>>>", "parse result of text is (" + matcher.group(1) + ")");
            return new Word(matcher.group(1), "");
        }
        return Word.NULL;
    }

    @Override
    public String toString() {
        return getEng() + "@" + getTranslation();
    }
}
